package bal.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ContratarAgenteActivity extends Activity{

	private ListView lv;
	private String listaAgentes;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contratar_agente);
		
		listaAgentes = getAgentes();
		String agentes[];
		
		agentes = listaAgentes.split(";");
		
		lv = (ListView) findViewById(R.id.listView1);
		lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, agentes));
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, final int position, long id) {
				final AlertDialog.Builder adb = new AlertDialog.Builder(ContratarAgenteActivity.this);
				adb.setTitle("Agente selecionado");
				
				final String agenteSelecionado = (String)lv.getItemAtPosition(position);
				
				adb.setMessage("Voce escolheu " + agenteSelecionado );
				adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				      public void onClick(DialogInterface dialog, int which) {
				          contratarAgente(agenteSelecionado);
				      } });
				adb.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					//  Action for 'NO' Button
					dialog.cancel();
					}
					});
				adb.show();
			}
		});
		
		Button voltar = (Button) findViewById(R.id.buttonVoltarAg);
		
		voltar.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				voltar();
			}
		});
		
	}
	
	public void voltar() {
		Intent intent = new Intent(this, HomeActivity.class);
		startActivity(intent);
		this.finish();
	}
	
	public String contratarAgente(String agente) {
		String[] keys = {"agente"};
		String[] values = {agente};
		
		String response = ConexaoServidor.execute("CadastrarAgenteServlet", keys, values);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		
		if(response.equals("Sucesso")) {
			
			builder.setMessage("Contratado com sucesso!");
			builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					voltar();
				}
			});
			builder.show();

		}
		else {
			
			if(response.equals("erro")) {

				builder.setMessage("Ocorreu um erro.");
				builder.setNeutralButton("Voltar", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
				builder.show();
			}
			else {
				builder.setMessage(response);
				builder.setNeutralButton("Voltar", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
				builder.show();
			}
		}
		
		return response;
	}
	
	public String getAgentes() {
		String response = ConexaoServidor.execute("ListarAgenteServlet", null, null);
		return response;
	}
	
}
